﻿using System;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy;
using Volo.Abp.SecurityLog;
using Volo.Abp.Validation;

namespace IdentityManagement
{
    [Index(nameof(TenantId), nameof(ApplicationName))]
    [Index(nameof(TenantId), nameof(Identity))]
    [Index(nameof(TenantId), nameof(Action))]
    [Index(nameof(TenantId), nameof(UserId))]
    public class IdentitySecurityLog : AggregateRoot<Guid>, IMultiTenant
    {
        public Guid? TenantId { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxApplicationNameLength))]
        public string ApplicationName { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxIdentityLength))]
        public string Identity { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxActionLength))]
        public string Action { get; protected set; }

        public Guid? UserId { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxUserNameLength))]
        public string UserName { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxTenantNameLength))]
        public string TenantName { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxClientIdLength))]
        public string ClientId { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxCorrelationIdLength))]
        public string CorrelationId { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxClientIpAddressLength))]
        public string ClientIpAddress { get; protected set; }

        [DynamicMaxLength(typeof(IdentitySecurityLogConsts), nameof(IdentitySecurityLogConsts.MaxBrowserInfoLength))]
        public string BrowserInfo { get; protected set; }

        public DateTime CreationTime { get; protected set; }

        protected IdentitySecurityLog()
        {

        }

        public IdentitySecurityLog(IGuidGenerator guidGenerator, SecurityLogInfo securityLogInfo)
            : base(guidGenerator.Create())
        {
            TenantId = securityLogInfo.TenantId;
            TenantName = securityLogInfo.TenantName.Truncate(IdentitySecurityLogConsts.MaxTenantNameLength);

            ApplicationName = securityLogInfo.ApplicationName.Truncate(IdentitySecurityLogConsts.MaxApplicationNameLength);
            Identity = securityLogInfo.Identity.Truncate(IdentitySecurityLogConsts.MaxIdentityLength);
            Action = securityLogInfo.Action.Truncate(IdentitySecurityLogConsts.MaxActionLength);

            UserId = securityLogInfo.UserId;
            UserName = securityLogInfo.UserName.Truncate(IdentitySecurityLogConsts.MaxUserNameLength);

            CreationTime = securityLogInfo.CreationTime;

            ClientIpAddress = securityLogInfo.ClientIpAddress.Truncate(IdentitySecurityLogConsts.MaxClientIpAddressLength);
            ClientId = securityLogInfo.ClientId.Truncate(IdentitySecurityLogConsts.MaxClientIdLength);
            CorrelationId = securityLogInfo.CorrelationId.Truncate(IdentitySecurityLogConsts.MaxCorrelationIdLength);
            BrowserInfo = securityLogInfo.BrowserInfo.Truncate(IdentitySecurityLogConsts.MaxBrowserInfoLength);

            foreach (var property in securityLogInfo.ExtraProperties)
            {
                ExtraProperties.Add(property.Key, property.Value);
            }
        }
    }
}
